#include "Evaluation.h";

int evaluation(char *expression){
	Expression e1=evaluateWithOperand(*expression);
	return e1.eval();
}

Expression evaluateWithOperand(char *expression){
	int value;
	Expression e1=null;
	while(*expression){
		value=exctractInt(*expression);
		*expression++;
		while((*expression=='*') || (*expression=='/')){
			if(*expression=='*'){
				*expression++;
				if(e1!=null) e1=new Mult(new Leaf(value), new Leaf(exctractInt(*expression)));
				else e1=new Mult(e1, exctractInt(*expression));
			}
			else{
				*expression++;
				f(e1!=null) e1=new Div(new Leaf(value), new Leaf(exctractInt(*expression)));
				else e1=new Div(e1, exctractInt(*expression));
			}
		}
		if(*expression=='+'){
			if(e1!=null) return new Plus(e1, evaluateWithOperand(*expression));
			return new Plus(new Leaf(value), evaluateWithOperand(*expression));
		}
		if(*expression=='-'){
			if(e1!=null) return new Minus(e1, evaluateWithOperand(*expression));
			return new Minus(new Leaf(value), evaluateWithOperand(*expression));
		}
	}
}

int extractInt(char *expression){
	String value;
	while((*expression!='*') || (*expression!='/') || (*expression!='+') || (*expression!='-')){
		value+=*expression;
		*expression++;
	}
	return atoi(value);
}
